from openai import OpenAI
import os


# os.environ["API_KEY"]="替换为自己的apikey或者在环境中进行配置"


import base64

#  base 64 编码格式
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def get_response(image_path,text):
    #输入参数：本地图片路径,需求文本
    #返回参数：模型返回的文本

    base64_image = encode_image(image_path)
    client = OpenAI(
        api_key=os.getenv("API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )
    completion = client.chat.completions.create(
        model="qwen-vl-plus",#调用的模型名称，可以根据阿里云文档里数据进行替换
        top_p=0.7,
        messages=[
            {
              "role": "user",
              "content": [
                {
                  "type": "text",
                  "text": text#根据你的需求进行修改
                },
                {
                  "type": "image_url",
                  "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"
                  }
                }
              ]
            }
          ],
          # stream=True,#是否流式传输
          # stream_options={"include_usage":True}#流式传输的选项
        )
    # for chunk in completion:#流式传输的代码
    #     print(chunk.model_dump())#打印每一块内容
    return_text=completion.choices[0].message.content
    print(return_text)
    return return_text








if __name__=='__main__':
    text=get_response("1.png","告诉我图片中有哪些消息，并进行以下操作………………")


